﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExcelLibrary.SpreadSheet;
using System.Threading;
using System.IO;

namespace PatentFeeManage
{
    public partial class FrImportExcel : Form
    {
        /// <summary>
        /// 消息信息列表。
        /// </summary>
        private List<string> messages = new List<string>();

        /// <summary>
        /// 消息信息列表操作锁。
        /// </summary>
        private readonly object messageLock = new object();

        /// <summary>
        /// 操作消息的线程。
        /// </summary>
        private Thread messageThread;

        /// <summary>
        /// 导入线程。
        /// </summary>
        private Thread importThread;

        /// <summary>
        /// 是否导入完成。
        /// </summary>
        private bool isImportFinish = false;

        public FrImportExcel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 点击选择按钮引发的事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            if (ofdExcel.ShowDialog() == DialogResult.OK)
            {
                tbFile.Text = ofdExcel.FileName;
            }
        }

        /// <summary>
        /// 端机取消按钮引发的事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 点击导入按钮引发的事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(tbFile.Text))
                {
                    btnImport.Enabled = false;
                    btnOpenFile.Enabled = false;
                    btnCancel.Enabled = false;

                    FileStream fs = File.OpenRead(tbFile.Text);

                    FileStream fs1 = File.Open("../../fileTemp/" + tbFile.Text.Split('\\')[tbFile.Text.Split('\\').Length - 1], FileMode.Create, FileAccess.ReadWrite);

                    fs.CopyTo(fs1);

                    fs.Close();
                    fs.Dispose();
                    fs1.Close();
                    fs1.Dispose();

                    messageThread = new Thread(OutputMessage);
                    messageThread.Start();

                    importThread = new Thread(ImportExcel);
                    importThread.Start();

                    btnImport.Enabled = true;
                    btnOpenFile.Enabled = true;
                    btnCancel.Enabled = true;
                }
                else
                {
                    MessageBox.Show("未选择文件。");
                }
            }
            catch (Exception ex) { rtbOutput.AppendText(ex.StackTrace); }
        }

        /// <summary>
        /// 导入excel信息。
        /// </summary>
        /// <param name="filePath"></param>
        private void ImportExcel()
        {
            var workbook = new Workbook();
            workbook = Workbook.Open("../../fileTemp/" + tbFile.Text.Split('\\')[tbFile.Text.Split('\\').Length - 1]);

            if (workbook.Worksheets.FirstOrDefault(p => p.Name == "基本信息") != null)
            {

            }

            foreach (var sheet in workbook.Worksheets)
            {

            }

            isImportFinish = true;
        }

        /// <summary>
        /// 写消息信息
        /// </summary>
        /// <param name="message">消息。</param>
        private void WriteMessage(string message)
        {
            lock (messageLock)
            {
                messages.Add(message);
            }
        }

        /// <summary>
        /// 输出消息信息。
        /// </summary>
        private void OutputMessage()
        {
            while (true)
            {
                lock (messageLock)
                {
                    if (messages.Count > 0)
                    {
                        foreach (var message in messages)
                        {
                            messages.Remove(message);
                            rtbOutput.AppendText(messages + "\n");
                        }
                    }
                }
                if (isImportFinish && messages.Count == 0)
                {
                    break;
                }
                Thread.Sleep(500);
            }
        }
    }
}
